import {
	uploadOssFiles
} from "@/api/user";
let formData = {}
let videoUrl = ''

export default (type = 'post') => {
	videoUrl = ''
	return new Promise(async (resolve, rejects) => {
		try {
			const {
				host,
				dir,
				filename,
				policy,
				accessId,
				signature
			} = await uploadOssFiles(type)
			formData = {
				action: host, // oss上传地址
				key: dir + filename, // oss上传文件名
				policy, // oss上传策略
				OSSAccessKeyId: accessId, // oss上传 accessKeyId
				signature // oss上传签名
			}
			await uni.chooseVideo({
				maxDuration: 60,
				sourceType: ["album", "camera"],
				success: (res) => {
					uni.showLoading({
						title: '上传中',
						mask: true
					})
					uni.uploadFile({
						url: host,
						filePath: res.tempFilePath,
						formData,
						name: "file",
						header: {
							"Content-Type": "multipart/form-data"
						},
						success: (arr) => {
							uni.hideLoading()
							if (arr.statusCode !== 204) {
								// 视频上传失败了
								return rejects(arr.errMsg);
							}
							videoUrl = host + "/" + dir + filename;
							return resolve(videoUrl)
						},
					});
				},
				fail: function(err) {
					rejects('上传失败')
					uni.hideLoading()
					console.log(err, '上传失败上传失败');
				},
			});


		} catch (e) {
			return rejects(e, 'ee上传失败')
		}

	})
}